package ejemplo.transformacion.xsl.core;

import java.io.*;

import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;

/**
 * 
 * @author Roberto Canales
 */
public class Transformador {
	
	String xmlNameFile ;
	String xslNameFile ;
	private boolean jeeProject;

	
	
	public Transformador(String xmlNameFile, String xslNameFile) {
		this(xmlNameFile, xslNameFile, true);
		
	}
	public Transformador(String xmlNameFile, String xslNameFile,
			boolean jeeProject) {
		super();
		this.xmlNameFile = xmlNameFile;
		this.xslNameFile = xslNameFile;
		this.jeeProject = jeeProject;
	}



	public String transformar() throws Exception {
		String explode="/bin/";
		if(jeeProject)explode="/build/classes/";
		File fileXMLOrigen = new File(System.getProperty("user.dir")+explode+xmlNameFile);
		File fileXSLOrigen = new File(System.getProperty("user.dir")+explode+xslNameFile);

		StringReader reader = new StringReader("<xml>blabla</xml>");
		Source xmlSource = new StreamSource(reader);
		Source xsltSource = new StreamSource(fileXSLOrigen);

		StringWriter cadenaSalida = new StringWriter();

		Result bufferResultado = new StreamResult(cadenaSalida);

		TransformerFactory factoriaTrans = TransformerFactory.newInstance();
		Transformer transformador = factoriaTrans.newTransformer(xsltSource);

		transformador.transform(xmlSource, bufferResultado);

		return cadenaSalida.toString();
	}
}